using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

Console.WriteLine("直接API测试应用程序启动");

// 创建HttpClient实例
using var httpClient = new HttpClient();
// 设置API基础地址（从MauiApp的配置中看到的默认值）
httpClient.BaseAddress = new Uri("http://localhost:5269/api/");

// 测试获取所有文件及其时间戳的API端点
Console.WriteLine("\n开始测试 file 端点");
try
{
    var response = await httpClient.GetAsync("file");
    if (response.IsSuccessStatusCode)
    {
        var content = await response.Content.ReadAsStringAsync();
        Console.WriteLine("API响应状态码: " + response.StatusCode);
        Console.WriteLine("API响应内容: " + content);
        
        // 尝试解析JSON响应
        try
        {
            using var doc = JsonDocument.Parse(content);
            var root = doc.RootElement;
            Console.WriteLine("成功解析JSON响应");
            
            if (root.ValueKind == JsonValueKind.Array)
            {
                Console.WriteLine($"响应包含 {root.GetArrayLength()} 个文件项");
                
                // 打印前几个文件的基本信息
                int count = 0;
                foreach (var item in root.EnumerateArray())
                {
                    if (count >= 5) break; // 只打印前5个
                    
                    Console.WriteLine($"\n文件 {count + 1}:");
                    
                    if (item.TryGetProperty("id", out var idProp))
                        Console.WriteLine($"ID: {idProp}");
                    
                    if (item.TryGetProperty("fileName", out var fileNameProp))
                        Console.WriteLine($"文件名: {fileNameProp}");
                    
                    if (item.TryGetProperty("savedPath", out var savedPathProp))
                        Console.WriteLine($"保存路径: {savedPathProp}");
                    
                    if (item.TryGetProperty("lastTimestamp", out var lastTimestampProp) && lastTimestampProp.ValueKind != JsonValueKind.Null)
                    {
                        Console.WriteLine("最后时间戳:");
                        if (lastTimestampProp.TryGetProperty("time", out var timeProp))
                            Console.WriteLine($"  时间: {timeProp}");
                        if (lastTimestampProp.TryGetProperty("openTime", out var openTimeProp))
                            Console.WriteLine($"  打开时间: {openTimeProp}");
                    }
                    else
                    {
                        Console.WriteLine("最后时间戳: 无");
                    }
                    
                    count++;
                }
            }
        }
        catch (JsonException ex)
        {
            Console.WriteLine($"JSON解析错误: {ex.Message}");
        }
    }
    else
    {
        Console.WriteLine($"API调用失败，状态码: {response.StatusCode}");
        var errorContent = await response.Content.ReadAsStringAsync();
        Console.WriteLine($"错误内容: {errorContent}");
    }
}
catch (Exception ex)
{
    Console.WriteLine($"请求发生异常: {ex.Message}");
    Console.WriteLine($"异常详情: {ex.InnerException?.Message}");
}

Console.WriteLine("\n测试完成，按任意键退出...");
Console.ReadKey();